class GameSystemHRD extends Behaviour {

    object = null;
    info = {
        position: null,
        data: [
            [1, 1, 1, 1, 1, 1],
            [1, 0, 0, 0, 0, 1],
            [1, 0, 0, 0, 0, 1],
            [1, 0, 0, 0, 0, 1],
            [1, 0, 0, 0, 0, 1],
            [1, 0, 0, 0, 0, 1],
            [1, 1, 1, 1, 1, 1]
        ]
    };
    result = {};
    target = null;
    system = CollisionDetectionSystem;
    gameResult = false;

    onAddToGameObject() {
        this.setData();
    }

    setData() {
        if (this.target) {
            this.object = {
                length: this.target.getBehaviour(CanMoveButton)._length,
                height: this.target.getBehaviour(CanMoveButton)._height
            };
            this.info.position = {
                x: this.target.getBehaviour(CanMoveButton).x,
                y: this.target.getBehaviour(CanMoveButton).y
            };
        }
        this.system.setData(this.object, this.info.position, this.info.data);
    }

    initData(x, y, length, height, number) {

        for (let i = 0; i < height; i++) {
            for (let j = 0; j < length; j++) {
                this.info.data[y + i][x + j] = number;
            }
        }
        //console.log(this.info.data)
    }


    onUpdate() {
        this.result = this.system.result;

        //console.log(this.info.position)

        if (this.target && this.gameResult == false) {
            if (this.target.id == "chengxuyuan" && this.info.position.x == 2 && this.info.position.y == 5) {
                this.gameResult = true;
            }
        }

        //console.log(this.info.data);
    }
}

class GameInitSystemHRD extends Behaviour {

    image = null;
    data = null;

    onStart() {
        //console.log(this)
        this.getData();
        this.initGameObject();
    }

    getData() {
        this.image = gameProcess.image;
        this.data = gameProcess.gameData;
    }

    initGameObject() {
        const parent = core.getObjectById("objects");
        const keys = Object.keys(this.data);

        for (let key of keys) {

            const obj = new GameObject();
            const behaviour = new CanMoveButton();
            const transform = new Transform();
            obj.id = key;
            behaviour.x = this.data[key].x;
            behaviour.y = this.data[key].y;
            switch (this.data[key].type) {
                case "chengxuyuan":
                    behaviour.image = this.image.chengxuyuan;
                    behaviour._length = 2;
                    behaviour._height = 1;
                    break;
                case "bug":
                    behaviour.image = this.image.bug;
                    behaviour._length = 1;
                    behaviour._height = 2;
                    break;
                case "error":
                    behaviour.image = this.image.error;
                    behaviour._length = 2;
                    behaviour._height = 1;
                    break;
                case "404":
                    behaviour.image = this.image.e404;
                    behaviour._length = 1;
                    behaviour._height = 1;
                    break;
            }
            transform.x = this.data[key].x * 100 - 100;
            transform.y = this.data[key].y * 100 - 100;

            obj.addBehaviour(transform);
            obj.addBehaviour(behaviour);
            parent.addChild(obj);
            //console.log(key)
            //core.getObjectById(key).getBehaviour(CanMoveButton).run();
        }
    }
}

class MoveButton extends Button {

    @SerializedField('')
    direction = "";

    onStart() {

        this.onClick = () => {

            const system = core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD);
            const result = core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD).result;
            const target = core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD).target;

            // console.log(result);

            switch (this.direction) {
                case 'up':
                    if (result.up.thisObject) {
                        target.getBehaviour(CanMoveButton).initData(0);
                        target.getBehaviour(Transform).y -= 100;
                        target.getBehaviour(CanMoveButton).y -= 1;
                        target.getBehaviour(CanMoveButton).initData(1);
                        system.setData();
                    }
                    break;
                case 'down':
                    if (result.down.thisObject) {
                        target.getBehaviour(CanMoveButton).initData(0);
                        target.getBehaviour(Transform).y += 100;
                        target.getBehaviour(CanMoveButton).y += 1;
                        target.getBehaviour(CanMoveButton).initData(1);
                        system.setData();
                    }
                    break;
                case 'left':
                    if (result.left.thisObject) {
                        target.getBehaviour(CanMoveButton).initData(0);
                        target.getBehaviour(Transform).x -= 100;
                        target.getBehaviour(CanMoveButton).x -= 1;
                        target.getBehaviour(CanMoveButton).initData(1);
                        system.setData();
                    }
                    break;
                case 'right':
                    if (result.right.thisObject) {
                        target.getBehaviour(CanMoveButton).initData(0);
                        target.getBehaviour(Transform).x += 100;
                        target.getBehaviour(CanMoveButton).x += 1;
                        target.getBehaviour(CanMoveButton).initData(1);
                        system.setData();
                    }
                    break;
            }
        }
    }
}

class CanMoveButton extends Button {

    id = "";
    // @SerializedField(0)
    x = 0;
    // @SerializedField(0)
    y = 0;
    // @SerializedField(0)
    _length = 0;
    // @SerializedField(0)
    _height = 0;

    onAddToGameObject() {

        super.onAddToGameObject();

        this.id = this.gameObject.id;
        this.onClick = () => {
            console.log(this.gameObject.id);
            core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD).target = this.gameObject;
            core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD).setData();
        }

        this.initData(1);

    }

    initData(number) {
        core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD).initData(this.x, this.y, this._length, this._height, number);
    }

}

class ReStartButton extends Button {

    onStart() {
        this.onClick = () => {
            core.changeScene("./data/scenes/HuaRongDao.json");
        }
    }
}

class FinishGameButton extends Button {

    onStart() {
        this.onClick = () => {
            gameProcess.setGameResult(core.getObjectById("gameSystemHRD").getBehaviour(GameSystemHRD).gameResult);
            gameProcess.changeSceneBack("morework");
        }
    }
}


core.registerBehaviourClass(GameSystemHRD);
core.registerBehaviourClass(GameInitSystemHRD);
core.registerBehaviourClass(MoveButton);
core.registerBehaviourClass(CanMoveButton);
core.registerBehaviourClass(ReStartButton);
core.registerBehaviourClass(FinishGameButton);